package com.sinosoft.oms.service.impl;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author:Frankiegg
 * @date:2018/5/24
 * @project:bee
 * @Modified By:
 **/
public class testSql {
    public static String replaceStr(String sourceStr,String replaceKey,String replaceValue){
        String replaceStrReg="";
        for(char str_char:replaceKey.toCharArray()){
            replaceStrReg+="[";
            replaceStrReg+=str_char;
            replaceStrReg+="]";
        }
        String startReg="^"+replaceStrReg+"([\\+\\-\\*/,)])";
        String endReg="([\\+\\-\\*/,(])"+replaceStrReg+"$";
        String reg="([^a-zA-Z])("+replaceStrReg+")"+"([^a-zA-Z])";
        String endStr=sourceStr;
        while(matcheStr(endStr,replaceKey)){
            endStr=endStr.replaceAll(startReg, replaceValue+"$1");
            endStr=endStr.replaceAll(reg, "$1"+replaceValue+"$3");
            endStr=endStr.replaceAll(endReg, "$1"+replaceValue);
        }
        return endStr;
    }

    public static Boolean matcheStr(String sourceStr,String matchStr){
        String replaceStrReg="";
        for(char str_char:matchStr.toCharArray()){
            replaceStrReg+="[";
            replaceStrReg+=str_char;
            replaceStrReg+="]";
        }
        String startReg="^"+replaceStrReg+"([\\+\\-\\*/,)])[\\s\\S]*";
        String endReg="[\\s\\S]*([\\+\\-\\*/,(])"+replaceStrReg+"$";
        String reg="[\\s\\S]*([^A-Za-z])("+replaceStrReg+")"+"([^A-Za-z])[\\s\\S]*";
        if(sourceStr.matches(startReg)||sourceStr.matches(reg)||sourceStr.matches(endReg)){
            return true;
        }else{
            return false;
        }
    }

    public static String Calculation(String formula){
        String result;
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        try {
            result =String.valueOf(engine.eval(formula));
        } catch (ScriptException e) {
            result="Nan";
        }
        return result;
    }

    public static void main(String[] args){
//        String aaa= replaceStr("Prem/2","Prem","2");
//        System.out.println(aaa);
//        String bbb=Calculation(aaa);
        splitWord("(Prem+age+3)/2");
//        System.out.println(bbb);
    }

    public static void splitWord(String word){
        String s ="[a-zA-Z]+";
        Pattern pattern = Pattern.compile(s);
        Matcher m = pattern.matcher(word);
        ArrayList<String> al =new ArrayList();
        while(m.find()){
            al.add(m.group(0));
        }
        System.out.println(al.size());
        for (String aaa:al){
            System.out.println(aaa);
        }
    }


}
